package com.bhs.sansong.ui.wallet;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.TotalBillAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.BillingDetails;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 总账单
 */
public class TotalBillActivity extends BaseActivity implements NetCallBack {

    private RecyclerView mRvList;
    private int page = 1;
    private TotalBillAdapter mAdapter;
    private SwipeRefreshLayout mRefreshLayout;

    @Override
    public int initLayout() {
        return R.layout.activity_total_bill;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("我的账单");
        mRvList = findViewById(R.id.rv_list);
        mAdapter = new TotalBillAdapter();
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
        mRefreshLayout = findViewById(R.id.refreshLayout);
    }

    @Override
    public void initData() {
        loadData();
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(mActivity).inflate(R.layout.layout_empty, null));

    }

    private void loadData() {
        Map<String, String> map = new ArrayMap<>();
        map.put("page", String.valueOf(page));
        map.put("limit", "10");
        map.put("platform", "cargo");
        networkRequest(netApi.get_bill_list(map), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BillingDetails bean = new Gson().fromJson(json, BillingDetails.class);
        mRefreshLayout.setRefreshing(false);
        if (bean.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}